Firstly, the same example as on the main 4.3.11 page, only showing the usefulness of the resetNext( ) method.
Plus showing a collection/list of user made objects.
public static void main(String[] args) { 3 OurList<String> list = new OurList<String>(); 4 list.addItem(new String("hello")); 5 list.addItem(new String("world")); 6 list.addItem(new String("how are you?")); 7 list.resetNext(); 8 if (!list.isEmpty()) { 9 while (list.hasNext()) { 10 System.out.println(list.getNext()); 11 } 12 } else { 13 System.out.println("List is empty"); 14 } 15 list.resetNext(); // Here, we go back to the beginning, with reset( ) 16 System.out.println("The first in the list, once again is: " + list.getNext()); 17 System.out.println(); 18 //And here, we show it's the same sort of thing with OOP (for when OOP is done). 19 OurList<StudentClass> studentList = new OurList<StudentClass>(); 20 studentList.addItem(new StudentClass("Sally", 17, true)); 21 studentList.addItem(new StudentClass("Bob", 18, false)); 22 System.out.println(studentList.getNext().getName()); 23 }
Output:
hello
world
how are you?
The first in the list, once again is: hello
Sally